/**
 * 获取url地址栏指定参数
 * @returns {*param}
 */
export const getUrlParam = function (name) {
  //获取？后面的参数
  let urlSearch = location.search || location.hash;
  let urlValue = '';
  //以？*&来拆分
  let params = urlSearch.split(/[?*&]/);
  for (let i = 0; i < params.length; i++) {
    //如果url参数里包含传递过来name字段，则取=后面的部分
    if (params[i].indexOf(name) >= 0) {
      urlValue = params[i].split('=')[1];
      return urlValue;
    }
  }
  return urlValue;
};

/**
 * 将秒转化成时分秒
 */
export const formatSeconds = function (value) {
  var theTime = parseInt(value); // 秒
  var middle = 0; // 分
  var hour = 0; // 小时

  if (theTime > 60) {
    middle = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    if (middle > 60) {
      hour = parseInt(middle / 60);
      middle = parseInt(middle % 60);
    }
  }
  var result = '' + parseInt(theTime);
  if (middle > 0) {
    result = '' + parseInt(middle) + result;
  }
  if (hour > 0) {
    result = '' + parseInt(hour) + result;
  }
  return result;
};

/** 改变url参数，传入键值对 */
export const updateUrlParams = function (data) {
  const url = new URL(window.location.origin + window.location.pathname);
  Object.keys(data).forEach(key => {
    url.searchParams.append(key, data[key]);
  });
  window.history.pushState({}, '', url);
};

/** 判断url是否有该参数 */
export const hasUrlParam = function (key) {
  const url = new URL(location.href);
  return url.searchParams.has(key);
};

/**
 *检测传入的参数类型
 *需要进行参数检测的对象
 *所属类型字符串
 */
export function typeOf(obj) {
  const toString = Object.prototype.toString;
  const map = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object'
  };
  if (obj instanceof Element) return 'element';
  return map[toString.call(obj)];
}

/**
 * 时间戳差值转化为时分秒
 */
export function getShowTime(timeDiff) {
  let days = parseInt(timeDiff / 86400000);
  let hours = parseInt((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  let minutes = parseInt((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
  let seconds = (timeDiff % (1000 * 60)) / 1000;
  if (days) {
    hours = days * 24 + hours;
  }
  hours < 10 ? (hours = '0' + hours) : hours; //小时格式化
  minutes < 10 ? (minutes = '0' + minutes) : minutes; //分钟格式化
  seconds < 10 ? (seconds = '0' + seconds) : seconds; //秒钟格式化
  return hours + minutes + seconds;
}

/**
 * 深拷贝对象
 * @param {*} obj 要拷贝的对象
 */
export const copy = function (obj) {
  // undefined
  if (obj === undefined) {
    return undefined;
  }
  // 其他
  return JSON.parse(JSON.stringify(obj));
};

/**
 * 计算图表横坐标显示标签的间隔数
 */
export function getInterval(params, len) {
    // 显示区间内显示标签的最大数
    const m = 10;
    // 显示区间范围
    const x = params.batch[0].end - params.batch[0].start;
    // 显示区间内标签数
    const y = Math.ceil(len * x / 100);
    // 显示区间内标签最大数
    const p = Math.ceil(y / m) * m;
    // 间隔数（显示1个间隔N个）
    return (p / m) - 1;
}